﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class AccountNowDepositController : Controller
    {
        public ActionResult AccountNowDepositIndex()
        {
            ViewData["Deptment"] = GetDeptment();
            return View();
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDeptment()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            AccountNowDepositInfo info = new AccountNowDepositInfo();
            DataSet ds = info.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetList(PageVM pvm)
        {
            AccountNowDepositInfo info = new AccountNowDepositInfo();
            string condition = pvm.query;
            string depname = " and 1=1 ";
            string datetime = " and 1=1 ";
            if (condition.Split(',')[0].ToString() != "全部")
            {
                depname = " and 部门名称='" + condition.Split(',')[0].ToString() + "'";
            }
            if (condition.Split(',')[1].ToString() != "")
            {
                datetime = " and RDateTime<='" + Convert.ToDateTime(condition.Split(',')[1].ToString()).AddDays(1) + "'";
            }
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            Session["rp"] = pvm.rp;
            Session["page"] = pvm.page;
            DataSet list = info.GetList(pvm.rp, pvm.page, depname, datetime);
            jsonFlexiGridData.total = info.GetCount(depname, datetime);
            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                v = new List<string>();
                v.Add(item["rownumber"].ToString());
                v.Add(item["账号"].ToString());
                v.Add(item["工号"].ToString());
                v.Add(item["姓名"].ToString());
                v.Add(item["部门名称"].ToString());
                v.Add(item["开户日期"].ToString());
                v.Add(item["账户余额"].ToString());
                v.Add(item["封户否"].ToString());
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item["rownumber"].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 现金出纳表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string depname1, string datetime)
        {

            return EContent(depname1, datetime);
        }

        public ExcelContentResult EContent(string depname1, string datetime1)
        {
            AccountNowDepositInfo info = new AccountNowDepositInfo();
            StringBuilder sBuilder = new StringBuilder();
            string depname = " and 1=1 ";
            string datetime = " and 1=1 ";
            if (depname1 != "全部")
            {
                depname = " and 部门名称='" + depname1 + "'";
            }
            if (datetime1 != "")
            {
                datetime = " and RDateTime<='" + Convert.ToDateTime(datetime1).AddDays(1) + "'";
            }
            DataSet list = info.GetListAll(depname, datetime);
            int count = info.GetCount(depname, datetime);
            decimal allmoney = info.GetAllMoney(depname, datetime);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='7' style='font-size:18px; text-align:center;'>上海港集装箱公司外高桥码头公司食堂餐饮管理信息系统</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='7' style='font-size:25px; text-align:center;font-weight:bold'>帐户现存款</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='7' style='font-size:14px;text-align:left;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;统计日期：" +
                DateTime.Now.ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;指定日期：" + datetime1 + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;部门：" + depname1 + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px;'>");
            sBuilder.Append("<td style='text-align:center;'>帐号</td>");
            sBuilder.Append("<td style='text-align:center;'>工号</td>");
            sBuilder.Append("<td style='text-align:center;'>姓名</td>");
            sBuilder.Append("<td style='text-align:center;'>部门</td>");
            sBuilder.Append("<td style='text-align:center;'>开户日期</td>");
            sBuilder.Append("<td style='text-align:center;'>帐户余额</td>");
            sBuilder.Append("<td style='text-align:center;'>封户否</td>");
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px;'>");
                sBuilder.Append("<td style='text-align:center;'>" + item["账号"].ToString() + "</td>");
                sBuilder.Append("<td style='text-align:center;'>" + item["工号"].ToString() + "</td>");
                sBuilder.Append("<td style='text-align:center;'>" + item["姓名"].ToString() + "</td>");
                sBuilder.Append("<td style='text-align:center;'>" + item["部门名称"].ToString() + "</td>");
                sBuilder.Append("<td style='text-align:center;'>" + item["开户日期"].ToString() + "</td>");
                sBuilder.Append("<td style='text-align:center;'>" + item["账户余额"].ToString() + "</td>");
                sBuilder.Append("<td style='text-align:center;'>" + item["封户否"].ToString() + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("<br />");
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td colspan='7'>&nbsp;&nbsp;合计：" + allmoney.ToString() + "&nbsp;&nbsp;&nbsp;人次：" + count.ToString() + "</td></tr>");
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td colspan='7'>&nbsp;&nbsp;打印日期：" + DateTime.Now.ToString("yyyy/MM/dd") + "&nbsp;&nbsp;&nbsp;" +
                "打印时间：" + DateTime.Now.ToString("HH:mm:ss") + "&nbsp;&nbsp;&nbsp;制表人：" +
                ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName + "</td></tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "zengjianhuisong";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
